package org.avro;

import java.io.File;
import java.io.IOException;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Building Instructions: command-line
 * 
 * To clean build all components without testing and install them in your local
 * repository:
 * 
 * $ mvn clean install -DskipTests
 * 
 * To compile only:
 * 
 * $ mvn compile
 * 
 * To run tests:
 * 
 * $ mvn test
 * 
 * To install to local repo, including running tests:
 * 
 * $ mvn install
 * 
 * Other useful mvn commands:
 * 
 * $ mvn clean $ mvn validate $ mvn help:effective-pom $ mvn site $ mvn
 * generate-resources
 * 
 * 
 * 根据Schema文件生成协议,进行序列化与反序列化
 * 
 * @author donald 2018年3月11日 下午6:54:10
 */
public class SpecificMain {
	private static final Logger log = LoggerFactory.getLogger(SpecificMain.class);

	public static void main(String[] args) throws IOException {
		/*
		 * User user1 = new User(); user1.setName("donald");
		 * user1.setAge(256); // Leave favorite color null
		 * 
		 * // Alternate constructor User user2 = new User("rain", 7, "shaoqinghan@aliyun.com");
		 * 
		 * // Construct via builder User user3 =
		 * User.newBuilder().setName("jamel").setEmail("shaoqinghan@qq.com").
		 * setFavoriteNumber(null).build();
		 * 
		 * // Serialize user1 and user2 to disk File file = new
		 * File("users.avro"); DatumWriter<User> userDatumWriter = new
		 * SpecificDatumWriter<User>(User.class); DataFileWriter<User>
		 * dataFileWriter = new DataFileWriter<User>(userDatumWriter);
		 * dataFileWriter.create(user1.getSchema(), file);
		 * dataFileWriter.append(user1); dataFileWriter.append(user2);
		 * dataFileWriter.append(user3); dataFileWriter.close();
		 * 
		 * // Deserialize Users from disk DatumReader<User> userDatumReader =
		 * new SpecificDatumReader<User>(User.class); DataFileReader<User>
		 * dataFileReader = new DataFileReader<User>(file, userDatumReader);
		 * User user = null; while (dataFileReader.hasNext()) { // Reuse user
		 * object by passing it to next(). This saves us from // allocating and
		 * garbage collecting many objects for files with // many items. user =
		 * dataFileReader.next(user); System.out.println(user); }
		 */
	}
}
